<?php
$idobjetivo = $this->user = sfContext::getInstance()->getUser()->getAttribute('id_obs');
$q = Doctrine_Query::create()
        ->from('objetivo o')
        ->where('o.id_ob=?', $idobjetivo);
$objetivoseleccionado = $q->fetchOne();
$idproyecto = $objetivoseleccionado->getIdPro();
$fi = explode("-", $objetivoseleccionado->getPeriodos()->getFechai());
$ff = explode("-", $objetivoseleccionado->getPeriodos()->getFechaf());
$ai = $fi[0];
$mi = $fi[1];
$af = $ff[0];
$mf = $ff[1];

$idac = $this->user = sfContext::getInstance()->getUser()->getAttribute('id_ac');
$idre = $this->user = sfContext::getInstance()->getUser()->getAttribute('id_tr');
$idfo = $this->user = sfContext::getInstance()->getUser()->getAttribute('id_fo');

//datos de entrada
$ainicial = $ai;
$afinal = $af;
$minicial = $mi;
$mfinal = $mf;
//
$aux = $ainicial;
?>
<tr align="center">
    <td colspan="2" id="filascolor">TOTAL:</td>

    <?php
    $valortotal = 0;
    $lisdescrip = Doctrine::getTable('descripciongeneral')->createQuery('d')
            ->where('d.id_pro = ?', $idproyecto)
            ->andwhere('d.id_tp = ?', $idre)
            ->execute();
    if ($ai == $af) {
        while ($mi <= $mf) {
            $valorpormes = 0;
            foreach ($lisdescrip as $descri):
                $listaval = Doctrine::getTable('valor')->createQuery('v')
                        ->where('v.id_dg = ?', $descri->getIdDg())
                        ->andwhere('v.id_ac = ?', $idac)
                        ->andwhere('v.id_fo = ?', $idfo)
                        ->andwhere('v.mes = ?', $mi)
                        ->andwhere('v.anio = ?', $ai)
                        ->execute();

                foreach ($listaval as $valfo):
                    $valorpormes = $valorpormes + $valfo->getVal();
                endforeach;
            endforeach;
            ?>
            <td><?php echo $valorpormes ?></td>
            <?php
            $valortotal = $valortotal + $valorpormes;
            $mi = $mi + 1;
        }
    } else {
        //caso 2
        //cuando el periodo es superior a 2 anios 
        $totalanios = $af - $ai;
        if ($totalanios == 1) {

            while ($mi <= 12) {
                $valorpormes = 0;
                foreach ($lisdescrip as $descri):
                    $listaval = Doctrine::getTable('valor')->createQuery('v')
                            ->where('v.id_dg = ?', $descri->getIdDg())
                            ->andwhere('v.id_ac = ?', $idac)
                            ->andwhere('v.id_fo = ?', $idfo)
                            ->andwhere('v.mes = ?', $mi)
                            ->andwhere('v.anio = ?', $ai)
                            ->execute();

                    foreach ($listaval as $valfo):
                        $valorpormes = $valorpormes + $valfo->getVal();
                    endforeach;
                endforeach;
                ?>
                <td><?php echo $valorpormes ?></td>
                <?php
                $valortotal = $valortotal + $valorpormes;
                $mi = $mi + 1;
            }

            $aux = 1;
            while ($aux <= $mf) {

                $valorpormes = 0;
                foreach ($lisdescrip as $descri):
                    $listaval = Doctrine::getTable('valor')->createQuery('v')
                            ->where('v.id_dg = ?', $descri->getIdDg())
                            ->andwhere('v.id_ac = ?', $idac)
                            ->andwhere('v.id_fo = ?', $idfo)
                            ->andwhere('v.mes = ?', $aux)
                            ->andwhere('v.anio = ?', $af)
                            ->execute();

                    foreach ($listaval as $valfo):
                        $valorpormes = $valorpormes + $valfo->getVal();
                    endforeach;
                endforeach;
                ?>
                <td><?php echo $valorpormes ?></td>
                <?php
                $valortotal = $valortotal + $valorpormes;


                $aux++;
            }
        } else {
            //caso 3
            //cuando el periodo es superior a 3 anios 

            if ($totalanios > 1) {

                while ($mi <= 12) {
                    $valorpormes = 0;
                    foreach ($lisdescrip as $descri):
                        $listaval = Doctrine::getTable('valor')->createQuery('v')
                                ->where('v.id_dg = ?', $descri->getIdDg())
                                ->andwhere('v.id_ac = ?', $idac)
                                ->andwhere('v.id_fo = ?', $idfo)
                                ->andwhere('v.mes = ?', $mi)
                                ->andwhere('v.anio = ?', $ai)
                                ->execute();

                        foreach ($listaval as $valfo):
                            $valorpormes = $valorpormes + $valfo->getVal();
                        endforeach;
                    endforeach;
                    ?>
                    <td><?php echo $valorpormes ?></td>
                    <?php
                    $valortotal = $valortotal + $valorpormes;
                    $mi++;
                }


                $auxai = $ai + 1;
                $auxaf = $af - 1;
                while ($auxai <= $auxaf) {
                    for ($i = 1; $i <= 12; $i++) {
                        $valorpormes = 0;
                        foreach ($lisdescrip as $descri):
                            $listaval = Doctrine::getTable('valor')->createQuery('v')
                                    ->where('v.id_dg = ?', $descri->getIdDg())
                                    ->andwhere('v.id_ac = ?', $idac)
                                    ->andwhere('v.id_fo = ?', $idfo)
                                    ->andwhere('v.mes = ?', $i)
                                    ->andwhere('v.anio = ?', $auxai)
                                    ->execute();

                            foreach ($listaval as $valfo):
                                $valorpormes = $valorpormes + $valfo->getVal();
                            endforeach;

                        endforeach;
                        ?>
                        <td><?php echo $valorpormes ?></td>
                        <?php
                        $valortotal = $valortotal + $valorpormes;
                    }
                    $auxai++;
                }

                $aux = 1;
                while ($aux <= $mf) {
                    $valorpormes = 0;
                    foreach ($lisdescrip as $descri):
                        $listaval = Doctrine::getTable('valor')->createQuery('v')
                                ->where('v.id_dg = ?', $descri->getIdDg())
                                ->andwhere('v.id_ac = ?', $idac)
                                ->andwhere('v.id_fo = ?', $idfo)
                                ->andwhere('v.mes = ?', $aux)
                                ->andwhere('v.anio = ?', $af)
                                ->execute();

                        foreach ($listaval as $valfo):
                            $valorpormes = $valorpormes + $valfo->getVal();
                        endforeach;
                    endforeach;
                    ?>
                    <td><?php echo $valorpormes ?></td>
                    <?php
                    $valortotal = $valortotal + $valorpormes;


                    $aux++;
                }
            }
        }
    }
    ?>
    <td align="center"><?php echo $valortotal ?></td>
    <?php $this->user = sfContext::getInstance()->getUser()->setAttribute('totalrecurso', $valortotal); ?>
</tr>